import { AfterViewChecked, Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppConsts } from '@shared/AppConsts';
import { AppComponentBase } from '@shared/common/app-component-base';
import { CreateOrUpdateTaskInput, CodeRuleEditDto, CodeRuleServiceProxy, ProfileServiceProxy, TaskServiceProxy, CodeRuleFormatViewDto } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { RuleFormatCreateOrEditComponent } from '../../RuleFormatCreateOrEdit/RuleFormatCreateOrEdit.component';
import * as _ from 'lodash';
import { finalize } from 'rxjs/operators';


@Component({
  selector: 'createoreditcoderulemodal',
  templateUrl: './createoreditcoderulemodal.component.html',
  styleUrls: ['./createoreditcoderulemodal.component.css']
})
export class CreateoreditcoderulemodalComponent extends AppComponentBase {


  @ViewChild('createOrEditModal') modal: ModalDirective;
  @ViewChild('ruleFormatCreateOrEdit') ruleFormatCreateOrEdit: RuleFormatCreateOrEditComponent;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  display = false;
  active = false;
  saving = false;
  coderule: CodeRuleEditDto = new CodeRuleEditDto();
  _codeRuleFormatViewDto: CodeRuleFormatViewDto = new CodeRuleFormatViewDto();
  selectedrecord: any;
  codeRuleFormatViewDto = [];

  constructor(injector: Injector,
    private _taskService: TaskServiceProxy,
    private _coderuleService: CodeRuleServiceProxy,
    private _profileService: ProfileServiceProxy) {
    super(injector);
  }
  onShown(): void {

  }
  show(coderuiid?: number): void {
    if (!coderuiid) {
      this.primengTableHelper.totalRecordsCount = this.codeRuleFormatViewDto.length;
      this.primengTableHelper.records = this.codeRuleFormatViewDto;
      this.primengTableHelper.hideLoadingIndicator();
      this.active = true;
      this.modal.show();
    } else {
      this._coderuleService.getCodeRule(coderuiid).subscribe(resultdata => {
        this.coderule = resultdata;
        this.codeRuleFormatViewDto = resultdata.codeRuleFormats;
        this.active = true;
        this.modal.show();
      });
    }

  }

  save(): void {
    this.coderule.codeRuleFormats = this.codeRuleFormatViewDto;
    this._coderuleService.createOrUpdateCodeRule(this.coderule)
      .pipe(finalize(() => { this.saving = false; }))
      .subscribe(() => {
        this.notify.info(this.l('SavedSuccessfully'));
        this.close();
        this.modalSave.emit(null);
      });
  }
  close(): void {
    this.codeRuleFormatViewDto = [];
    this.coderule = new CodeRuleEditDto();
    this.active = false;
    this.modal.hide();
  }

  AddRuleFormart(): void {
    this.ruleFormatCreateOrEdit.show(this.codeRuleFormatViewDto, null);
  }
  EditRuleFormart(): void {
    if (!!this.selectedrecord) {
      //获取选择行数据
      this._codeRuleFormatViewDto = this.selectedrecord;
      this.ruleFormatCreateOrEdit.show(this.codeRuleFormatViewDto, this._codeRuleFormatViewDto);
    }
  }
  DeleteRuleFormart(): void {
    //获取选择行数据 删除
    if (!!this.selectedrecord) {
      this.message.confirm(
        this.l('删除此条规则'),
        this.l('AreYouSure'),
        isConfirmed => {
          if (isConfirmed) {
            let index = this.codeRuleFormatViewDto.indexOf(this.selectedrecord);
            this.codeRuleFormatViewDto.splice(index, 1);
          }
        }
      );
    }
  }
}
